package com.yowasa.xiuxian.engine;

import cn.hutool.json.JSONUtil;
import com.yowasa.xiuxian.engine.item.ItemInterface;
import com.yowasa.xiuxian.entity.bo.item.ItemAttr;
import com.yowasa.xiuxian.entity.bo.item.UserItemBo;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ItemTypeEnum;
import com.yowasa.xiuxian.exception.HandlerException;
import com.yowasa.xiuxian.mapper.XiuxianUserEquipMapper;
import com.yowasa.xiuxian.service.helper.ItemHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import com.yowasa.xiuxian.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ItemEngine {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private ItemHelper itemHelper;


    public String useItem(UserBO user, UserItemBo myItem) {
        ItemTypeEnum type = ItemTypeEnum.getById(myItem.getItemType());
        switch (type) {
            case DAN_YAO:
                return normalUse(user, myItem);
            case LING_YAO:
                return userLingYao(user, myItem);
            case MI_JI:
                return useMiJi(user, myItem);
            case SU_CAI:
                return "素材无法使用！";
            case WU_QI:
                return useWuqi(user, myItem);
            case TU_ZHI:
                return usrTuzhi(user,myItem);
            default:
                return "无法使用的道具[" + myItem.getItemName() + "]";
        }
    }

    private String usrTuzhi(UserBO user, UserItemBo myItem) {
        userHelper.checkCd(user);
        ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(myItem.getItemName());
        String msg = itemInterface.effect(user);
        userHelper.lockUser(user, RandomUtil.randomInt(10, 30), "正在学习"+itemInterface.getName()+"的锻造之法");
        userHelper.saveUserInfo(user);
        itemHelper.dropItem(myItem, 1);
        return msg;

    }

    private String useWuqi(UserBO user, UserItemBo myItem) {
        Long num = itemHelper.checkEqipNum(user);
        if (num >= 4) {
            throw new HandlerException("你已经携带了四件装备，装不下了");
        }

        //        userHelper.rebuildBattleInfo(user);
        ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(myItem.getItemName());
        String msg = itemInterface.effectEquip(user,myItem);
        itemInterface.effect(user);
//        userHelper.rebuildBattleInfo(user);
        userHelper.saveUserInfo(user);
        ItemAttr itemAttr = JSONUtil.toBean(myItem.getItemAttr(), ItemAttr.class);
        itemHelper.useEquip(myItem,itemAttr.getQuality());
        itemHelper.dropItem(myItem, 1);
        return msg;
    }

    private String useMiJi(UserBO user, UserItemBo myItem) {
        userHelper.checkCd(user);
        ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(myItem.getItemName());
        String msg = itemInterface.effect(user);
        userHelper.lockUser(user, RandomUtil.randomInt(30, 90), "正在研习秘籍");
        userHelper.saveUserInfo(user);
        itemHelper.dropItem(myItem, 1);
        return msg;
    }

    private String normalUse(UserBO user, UserItemBo myItem) {
        ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(myItem.getItemName());
        String msg = itemInterface.effect(user);
        userHelper.saveUserInfo(user);
        itemHelper.dropItem(myItem, 1);
        return msg;
    }

    private String userLingYao(UserBO user, UserItemBo myItem) {
        //check tag
        Integer tag = user.getUserConfig().getLingYaoTag().getOrDefault(myItem.getItemName(), 0);
        if (tag >= 1) {
            throw new HandlerException("你已经服用过[" + myItem.getItemName() + "]了，对你已经没有效果了");
        }
        ItemInterface itemInterface = CommonEngine.ITEM_MAP.get(myItem.getItemName());
        String msg = itemInterface.effect(user);
        user.getUserConfig().getLingYaoTag().put(myItem.getItemName(), tag + 1);
        userHelper.saveUserInfo(user);
        itemHelper.dropItem(myItem, 1);
        return msg;
    }
}
